home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 40.8 KB | 1,678 lines | [TEXT/KAHL] |
- /*
- File: OCEObjects.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __OCEOBJECTS__
- #include "OCEObjects.h"
- #endif
-
- #ifndef __IOSTREAM__
- #include "IOStream.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __SCRIPT__
- #include "Script.h" // for UpperText and LowerText
- #endif
-
- #ifndef __FILES__
- #include "Files.h" // for FSWrite & FSRead
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h" // for ASSERT macros
- #endif
-
- #ifndef __NEWDELETE__
- #include "NewDelete.h"
- #endif
-
- #ifndef __THREADUTILITIES__
- #include "ThreadUtilities.h"
- #endif
-
- #ifndef __ABSTRACTFILE__
- #include "AbstractFile.h"
- #endif
-
- #pragma segment OCEObjects
-
- /***********************************|****************************************/
-
- // internal validity checking
-
- static Boolean gInternalValidityChecking = false;
-
- void SetOCEObjectsInternalValidityChecking ( Boolean state ) { gInternalValidityChecking = state; }
- Boolean GetOCEObjectsInternalValidityChecking () { return gInternalValidityChecking; }
-
- /***********************************|****************************************/
-
- unsigned short
- TRString::GetLogicalLength ( const RString* p )
- {
- return p ? p->dataLength : 0;
- }
-
- /***********************************|****************************************/
-
- unsigned short
- TRString::GetPhysicalSize ( const RString* p )
- {
- return GetPhysicalSize ( p ? p->dataLength : 0 );
- }
-
- /***********************************|****************************************/
-
- RString*
- TRString::Allocate ( unsigned short logicalLength, CharacterSet script, const void* source )
- {
- logicalLength = ( logicalLength < kRStringMaxBytes ) ? logicalLength : kRStringMaxBytes;
- RString* string = (RString*) FAILNewPtrClear ( GetPhysicalSize ( logicalLength ) );
- ASSERT_RETURN_ZERO ( string != nil );
- string->charSet = script;
- string->dataLength = logicalLength;
-
- if ( source )
- ::BlockMove ( source, &string->body, logicalLength );
-
- string->body [ logicalLength ] = '\0'; // add null terminator
-
- return string;
- }
-
- /***********************************|****************************************/
-
- void
- TRString::Deallocate ( RString*& p )
- {
- if ( p )
- {
- ::DeallocatePtr( (Ptr) p );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( MemError () == noErr );
- #endif
- p = nil;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::IsValidIndex ( unsigned short i ) const
- {
- return ( i >= 0 ) && ( i < fString->dataLength );
- }
-
- /***********************************|****************************************/
-
- unsigned short
- TRString::GetLogicalSize ( unsigned short physicalSize )
- {
- return physicalSize - GetPhysicalSize ( (unsigned short) 0 );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::IsValid ( ConstRStringPtr r, RStringKind k )
- {
- return ( r != nil ) && ::OCEValidRString ( r, k );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::IsValid () const
- {
- return IsValid ( fString, fKind );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::IsValid ( const TRString* r )
- {
- return r != nil && r->IsValid ();
- }
-
- /***********************************|****************************************/
-
- #if defined ( INTERNAL_CHECKS )
-
- Boolean
- TRString::IsValidInternal ( const RString* r, RStringKind k )
- {
- return !gInternalValidityChecking || IsValid ( r, k );
- }
-
- #endif
-
- /***********************************|****************************************/
-
- TRString::TRString ():
- fString ( Allocate ( 0 ) ),
- fKind ( kOCEGenericSensitive )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( CharacterSet script, RStringKind kind ):
- fString ( Allocate ( 0, script ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const char* source, CharacterSet script, RStringKind kind ):
- fString ( Allocate ( ::strlen ( source ), script, source ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const StringPtr source, CharacterSet script, RStringKind kind ):
- fString ( Allocate ( source [ 0 ], script, source + 1 ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const void* source, unsigned short length, CharacterSet script, RStringKind kind ):
- fString ( Allocate ( length, script, source ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const TRString& string ):
- fString ( Allocate ( string.fString->dataLength, string.fString->charSet, &string.fString->body ) ),
- fKind ( string.fKind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const TRString& string, RStringKind newKind ):
- fString ( Allocate ( string.fString->dataLength, string.fString->charSet, &string.fString->body ) ),
- fKind ( newKind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const RString& string, RStringKind kind ):
- fString ( Allocate ( string.dataLength, string.charSet, &string.body ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::TRString ( const RString* string, RStringKind kind ):
- fString ( string ? Allocate ( GetLogicalLength ( string ), ( (RString*) string )->charSet, &( (RString*) string )->body ) : Allocate ( 0, smRoman ) ),
- fKind ( kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TRString::~TRString ()
- {
- Deallocate ( fString );
- }
-
- /***********************************|****************************************/
-
- TRString&
- TRString::operator = ( const TRString& that )
- {
- if ( ASSERT ( this != &that ) )
- {
- if ( ASSERT ( that.IsValid () ) )
- {
- Deallocate ( fString );
- fString = Allocate ( that.fString->dataLength, that.fString->charSet, &that.fString->body );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TRString&
- TRString::operator = ( const RString& string )
- {
- if ( ASSERT ( fString != &string ) )
- {
- Deallocate ( fString );
- fString = Allocate ( string.dataLength, string.charSet, &string.body );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TRString&
- TRString::operator = ( const RString* string )
- {
- if ( ASSERT ( string != nil ) )
- {
- operator = ( *string );
- }
- else
- {
- SetLength ( 0 );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TRString&
- TRString::operator = ( const char* string )
- {
- if ( ASSERT ( string != nil ) )
- {
- Deallocate ( fString );
- fString = Allocate ( ::strlen ( string ), smRoman, string );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
- else
- {
- SetLength ( 0 );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TRString&
- TRString::operator = ( const StringPtr string )
- {
- if ( ASSERT ( string != nil ) )
- {
- Deallocate ( fString );
- fString = Allocate ( string [ 0 ], smRoman, string + 1 );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
- else
- {
- SetLength ( 0 );
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TRString::operator const RString* () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return fString;
- }
-
- /***********************************|****************************************/
-
- TRString::operator const RString& () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- return *fString;
- }
-
- /***********************************|****************************************/
-
- TRString::operator const char* () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return (const char*) fString->body;
- }
-
- /***********************************|****************************************/
-
- TRString::operator const StringPtr () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return (const StringPtr) &fString->dataLength + sizeof ( fString->dataLength ) - 1;
- }
-
- /***********************************|****************************************/
-
- Byte&
- TRString::operator [] ( unsigned short index ) const
- {
- #if defined ( INTERNAL_CHECKS )
- if ( ASSERT ( IsValidInternal () ) && ASSERT ( IsValidIndex ( index ) ) )
- #else
- if ( ASSERT ( IsValidIndex ( index ) ) )
- #endif
- {
- return fString->body [ index ];
- }
- else
- {
- static Byte gBozoChar = '\0';
- return gBozoChar; // let the bozo read from or write to the bozo char
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::GetString ( RString& destination, unsigned short maxChars ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- const unsigned short sourceSize = fString->dataLength;
- destination.charSet = fString->charSet;
-
- if ( sourceSize > maxChars )
- destination.dataLength = maxChars;
- else
- destination.dataLength = sourceSize;
-
- ::BlockMove ( fString->body, destination.body, destination.dataLength );
-
- return destination.dataLength == fString->dataLength;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::GetString ( StringPtr string, unsigned short maxChars ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- const unsigned short sourceSize = fString->dataLength;
-
- if ( sourceSize > 0xFF )
- string [ 0 ] = 0xFF;
- else if ( sourceSize > maxChars )
- string [ 0 ] = maxChars;
- else
- string [ 0 ] = (unsigned char) sourceSize;
-
- ::BlockMove ( fString->body, string + 1, string [ 0 ] );
- return string [ 0 ] == fString->dataLength;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::GetString ( char* string, unsigned short maxChars ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- unsigned short sourceSize = fString->dataLength;
-
- if ( sourceSize > maxChars )
- sourceSize = maxChars;
-
- ::BlockMove ( fString->body, string, sourceSize );
- return sourceSize == fString->dataLength;
- }
-
- /***********************************|****************************************/
-
- RString*
- TRString::GetNewRString () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return Allocate ( fString->dataLength, fString->charSet, &fString->body );
- }
-
- /***********************************|****************************************/
-
- StringPtr
- TRString::GetNewStringPtr () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- unsigned short sourceSize = fString->dataLength;
-
- if ( sourceSize > 0xFF ) // make sure that we don’t overflow
- sourceSize = 0xFF;
-
- StringPtr string = (StringPtr) FAILNewPtrClear ( sourceSize + 2 ); // extra bytes are prefix length byte and null terminator
- ASSERT_RETURN_ZERO ( string != nil );
-
- ::BlockMove ( &fString->body, string, sourceSize );
- string [ 0 ] = sourceSize; // fill in prefix length byte
- string [ sourceSize + 1 ] = 0; // fill in null terminator
-
- return string;
- }
-
- /***********************************|****************************************/
-
- char*
- TRString::GetNewString () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- const unsigned short sourceSize = fString->dataLength + 1; // extra byte is null terminator
-
- char* string = (char*) FAILNewPtrClear ( sourceSize );
- ASSERT_RETURN_ZERO ( string != nil );
-
- ::BlockMove ( &fString->body, string, sourceSize );
-
- return string;
- }
-
- /***********************************|****************************************/
-
- unsigned short
- TRString::SetLength ( unsigned short newSize )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- RString* newString = Allocate ( newSize, fString->charSet, &fString->body );
-
- if ( newString )
- {
- Deallocate ( fString );
- fString = newString;
- return newSize;
- }
- else
- {
- return fString->dataLength;
- }
- }
-
- /***********************************|****************************************/
-
- void
- TRString::SetScript ( CharacterSet c )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN ( IsValidInternal () );
- #endif
- fString->charSet = c;
- }
-
- /***********************************|****************************************/
-
- void
- TRString::SetKind ( RStringKind kind )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN ( IsValidInternal () );
- #endif
- fKind = kind;
- }
-
- /***********************************|****************************************/
-
- void
- TRString::MakeUppercase ()
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN ( IsValidInternal () );
- #endif
- ::UpperText ( (Ptr) &fString->body, fString->dataLength );
- }
-
- /***********************************|****************************************/
-
- void
- TRString::MakeLowercase ()
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN ( IsValidInternal () );
- #endif
- ::LowerText ( (Ptr) &fString->body, fString->dataLength );
- }
-
- /***********************************|****************************************/
-
- long
- TRString::Compare ( const RString& string ) const
- {
- return ::OCERelRString ( fString, &string, fKind );
- }
-
- /***********************************|****************************************/
-
- long
- TRString::Compare ( const TRString& string ) const
- {
- return ::OCERelRString ( fString, string.fString, fKind );
- }
-
- /***********************************|****************************************/
-
- long
- TRString::Compare ( const char* string ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- return ::strcmp ( *this, string );
- }
-
- /***********************************|****************************************/
-
- long
- TRString::Compare ( const StringPtr string ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- return ::RelString ( *this, string, true, true );
- }
-
- /***********************************|****************************************/
-
- ostream&
- TRString::operator >> ( ostream& s ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- s << "["; s.write ( fString->body, fString->dataLength );
- if ( fString->charSet != smRoman )
- s << ", " << fString->charSet;
- s << "]";
- return s;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::Write ( TAbstractFile& f ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- unsigned short physicalSize = GetPhysicalSize ( fString );
- ASSERT_RETURN_ZERO ( f.Write ( physicalSize ) == noErr );
- ASSERT_RETURN_ZERO ( f.WriteDataIgnore ( fString, physicalSize ) == noErr );
- ASSERT_RETURN_ZERO ( f.Write ( fKind ) == noErr );
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TRString::Read ( TAbstractFile& f )
- {
- unsigned short physicalSize = 0;
- ASSERT_RETURN_ZERO ( f.Read ( physicalSize ) == noErr );
- ASSERT_RETURN_ZERO ( physicalSize >= GetPhysicalSize ( (unsigned short) 0 ) );
- RString* temp = Allocate ( GetLogicalSize ( physicalSize ) );
- ASSERT_RETURN_ZERO ( temp != nil );
- ASSERT_RETURN_ZERO ( f.ReadDataIgnore ( temp, physicalSize ) == noErr );
- Deallocate ( fString );
- fString = temp;
- ASSERT_RETURN_ZERO ( f.Read ( fKind ) == noErr );
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return true;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( CharacterSet script ):
- TRString ( script, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const char* source, CharacterSet script ):
- TRString ( source, script, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const StringPtr source, CharacterSet script ):
- TRString ( source, script, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const void* source, unsigned short length, CharacterSet script ):
- TRString ( source, length, script, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const TDirectoryName& string ):
- TRString ( string, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const DirectoryName& name ):
- TRString ( (const RString&) name, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const DirectoryName* name ):
- TRString ( (const RString*) name, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const TRString& string ):
- TRString ( string, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const RString& string ):
- TRString ( string, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::TDirectoryName ( const RString* string ):
- TRString ( string, kOCEDirName )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectoryName::~TDirectoryName ()
- {
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TCreationID::TCreationID ()
- {
- fID.source = fID.seq = 0;
- }
-
- /***********************************|****************************************/
-
- TCreationID::TCreationID ( unsigned long a, unsigned long b )
- {
- fID.source = a;
- fID.seq = b;
- }
-
- /***********************************|****************************************/
-
- TCreationID::TCreationID ( const TCreationID& that ):
- fID ( that.fID )
- {
- }
-
- /***********************************|****************************************/
-
- TCreationID::TCreationID ( const CreationID& that ):
- fID ( that )
- {
- }
-
- /***********************************|****************************************/
-
- TCreationID::TCreationID ( const CreationID* that )
- {
- if ( that )
- {
- fID = *that;
- }
- else
- {
- fID.source = 0;
- fID.seq = 0;
- }
- }
-
- /***********************************|****************************************/
-
- TCreationID::~TCreationID ()
- {
- }
-
- /***********************************|****************************************/
-
- TCreationID&
- TCreationID::operator = ( const TCreationID& that )
- {
- if ( ASSERT ( this != &that ) )
- {
- fID = that.fID;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TCreationID&
- TCreationID::operator = ( const CreationID& that )
- {
- fID = that;
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TCreationID::operator == ( const CreationID& that ) const
- {
- return
- fID.source == that.source &&
- fID.seq == that.seq;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TCreationID::IsNull () const
- {
- return
- fID.source == 0 &&
- fID.seq == 0;
- }
-
- /***********************************|****************************************/
-
- void
- TCreationID::GetID ( CreationID& id ) const
- {
- id = fID;
- }
-
- /***********************************|****************************************/
-
- void
- TCreationID::GetID ( unsigned long& a, unsigned long& b ) const
- {
- a = fID.source;
- b = fID.seq;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TCreationID::Read ( TAbstractFile& f )
- {
- return f.ReadDataIgnore ( &fID, sizeof ( fID ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TCreationID::Write ( TAbstractFile& f ) const
- {
- return f.WriteDataIgnore ( &((TCreationID*) this)->fID, sizeof ( fID ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TCreationID::operator >> ( ostream& s ) const
- {
- return s << fID.source << ':' << fID.seq;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TDirDiscriminator::TDirDiscriminator ()
- {
- fDiscriminator.signature = 0;
- fDiscriminator.misc = 0;
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator::TDirDiscriminator ( OCEDirectoryKind signature, unsigned long misc )
- {
- fDiscriminator.signature = signature;
- fDiscriminator.misc = misc;
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator::TDirDiscriminator ( const TDirDiscriminator& that ):
- fDiscriminator ( that.fDiscriminator )
- {
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator::TDirDiscriminator ( const DirDiscriminator& discriminator ):
- fDiscriminator ( discriminator )
- {
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator::TDirDiscriminator ( const DirDiscriminator* discriminator )
- {
- if ( discriminator )
- {
- fDiscriminator = *discriminator;
- }
- else
- {
- fDiscriminator.signature = 0;
- fDiscriminator.misc = 0;
- }
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator::~TDirDiscriminator ()
- {
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator&
- TDirDiscriminator::operator = ( const TDirDiscriminator& that )
- {
- if ( ASSERT ( this != &that ) )
- {
- fDiscriminator = that.fDiscriminator;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator&
- TDirDiscriminator::operator = ( const DirDiscriminator& discriminator )
- {
- fDiscriminator = discriminator;
- return *this;
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator&
- TDirDiscriminator::operator = ( const DirDiscriminator* discriminator )
- {
- if ( discriminator )
- {
- fDiscriminator = *discriminator;
- }
- else
- {
- fDiscriminator.signature = 0;
- fDiscriminator.misc = 0;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TDirDiscriminator&
- TDirDiscriminator::operator = ( const OCEDirectoryKind& that )
- {
- fDiscriminator.signature = that;
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirDiscriminator::operator == ( const DirDiscriminator& that ) const
- {
- return
- fDiscriminator.signature == that.signature &&
- fDiscriminator.misc == that.misc;
- }
-
- /***********************************|****************************************/
-
- void
- TDirDiscriminator::GetDiscriminator ( DirDiscriminator& discriminator ) const
- {
- discriminator = fDiscriminator;
- }
-
- /***********************************|****************************************/
-
- void
- TDirDiscriminator::GetDiscriminator ( OCEDirectoryKind& signature, unsigned long& misc ) const
- {
- signature = fDiscriminator.signature;
- misc = fDiscriminator.misc;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirDiscriminator::Read ( TAbstractFile& f )
- {
- return f.ReadDataIgnore ( &fDiscriminator, sizeof ( fDiscriminator ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirDiscriminator::Write ( TAbstractFile& f ) const
- {
- return f.WriteDataIgnore ( &((TDirDiscriminator*)this)->fDiscriminator, sizeof ( fDiscriminator ) ) == noErr;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TDirDiscriminator::operator >> ( ostream& s ) const
- {
- return s << fDiscriminator.signature << ':' << fDiscriminator.misc;
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TDirectory::TDirectory ():
- fName (),
- fDiscriminator ()
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory::TDirectory ( const TDirectoryName& name ):
- fName ( name ),
- fDiscriminator ()
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory::TDirectory ( const TDirectoryName& name, const TDirDiscriminator& discriminator ):
- fName ( name ),
- fDiscriminator ( discriminator )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory::TDirectory ( const TDirectory& that ):
- fName ( that.fName ),
- fDiscriminator ( that.fDiscriminator )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory::TDirectory ( const DirectoryName* directory, DirDiscriminator* discriminator ):
- fName ( directory ),
- fDiscriminator ( discriminator )
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory::~TDirectory ()
- {
- }
-
- /***********************************|****************************************/
-
- TDirectory&
- TDirectory::operator = ( const TDirectory& that )
- {
- if ( ASSERT ( this != &that ) )
- {
- fName = that.fName;
- fDiscriminator = that.fDiscriminator;
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirectory::operator == ( const TDirectory& that ) const
- {
- return
- fName == that.fName &&
- fDiscriminator == that.fDiscriminator;
- }
-
- /***********************************|****************************************/
-
- void
- TDirectory::SetName ( const TDirectoryName& name )
- {
- fName = name;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirectory::GetName ( TDirectoryName& name ) const
- {
- name = fName;
- return true;
- }
-
- /***********************************|****************************************/
-
- void
- TDirectory::SetDiscriminator ( const TDirDiscriminator& discriminator )
- {
- fDiscriminator = discriminator;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirectory::GetDiscriminator ( TDirDiscriminator& discriminator ) const
- {
- discriminator = fDiscriminator;
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirectory::Read ( TAbstractFile& f )
- {
- return fName.Read ( f ) && fDiscriminator.Read ( f );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TDirectory::Write ( TAbstractFile& f ) const
- {
- return fName.Write ( f ) && fDiscriminator.Write ( f );
- }
-
- /***********************************|****************************************/
-
- ostream&
- TDirectory::operator >> ( ostream& s ) const
- {
- return s << fName << " (" << fDiscriminator << ')';
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- PackedPathName*
- TPathName::Allocate ( unsigned short bytes )
- {
- return (PackedPathName*) FAILNewPtrClear ( bytes );
- }
-
- /***********************************|****************************************/
-
- PackedPathName*
- TPathName::Allocate ( const PackedPathName* p1 )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal ( p1 ) );
- #endif
- unsigned short size = GetPhysicalSize ( p1 );
- PackedPathName* p2 = Allocate ( size );
- ASSERT_RETURN_ZERO ( p2 != nil );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( ::OCECopyPackedPathName ( p1, p2, size ) == noErr );
- #else
- ::OCECopyPackedPathName ( p1, p2, size );
- #endif
- return p2;
- }
-
- /***********************************|****************************************/
-
- PackedPathName*
- TPathName::Allocate ( RStringPtr stringArray[], unsigned short entries )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( entries > 0 );
- ASSERT_RETURN_ZERO ( stringArray != nil );
- for ( unsigned short jndex = 0; jndex < entries; jndex++ )
- ASSERT_RETURN_ZERO ( TRString::IsValid ( stringArray [ jndex ], kOCEGenericSensitive ) );
- #endif
- unsigned short pathNameSize = ::OCEPackedPathNameSize ( stringArray, entries );
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( pathNameSize >= sizeof ( ProtoPackedPathName ) );
- #endif
- PackedPathName* pathName = (PackedPathName*) Allocate ( pathNameSize );
- ASSERT_RETURN_ZERO ( pathName != nil );
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( ::OCEPackPathName ( stringArray, entries, pathName, pathNameSize ) == noErr );
- #else
- ::OCEPackPathName ( (const RStringPtr []) stringArray, entries, pathName, pathNameSize );
- #endif
- return pathName;
- }
-
- /***********************************|****************************************/
-
- ConstRStringPtr*
- TPathName::AllocateArray ( const TRString* objectArray [], unsigned short entries )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( entries > 0 );
- ASSERT_RETURN_ZERO ( objectArray != nil );
- for ( unsigned short jndex = 0; jndex < entries; jndex++ )
- ASSERT_RETURN_ZERO ( TRString::IsValid ( objectArray [ jndex ] ) );
- #endif
- ConstRStringPtr* stringArray = (ConstRStringPtr*) FAILNewPtrClear ( entries * sizeof ( ConstRStringPtr ) );
- ASSERT_RETURN_ZERO ( stringArray != nil );
- for ( unsigned short index = 0; index < entries; index++ )
- stringArray [ index ] = *objectArray [ index ];
- return stringArray;
- }
-
- /***********************************|****************************************/
-
- ConstRStringPtr*
- TPathName::AllocateArray ( const PackedPathName* packed )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal ( packed ) );
- #endif
- unsigned short entries = (packed && (packed->dataLength > 0) &&
- OCEValidPackedPathName(packed) ) ? ::OCEDNodeNameCount ( packed ) : 0;
- ConstRStringPtr stringArray = (ConstRStringPtr) FAILNewPtrClear ( entries * sizeof ( ConstRStringPtr ) );
- ASSERT_RETURN_ZERO ( stringArray != nil );
- if (entries > 0)
- ASSERT_RETURN_ZERO ( ::OCEUnpackPathName ( packed, * ( RStringPtr * *) & stringArray, entries ) == entries );
- return * ( ConstRStringPtr ** ) &stringArray;
- }
-
- /***********************************|****************************************/
-
- PackedPathName*
- TPathName::Allocate ( const TRString* objectArray [], unsigned short entries )
- {
- const RString ** stringArray = AllocateArray ( objectArray, entries );
- ASSERT_RETURN_ZERO ( stringArray != nil );
- PackedPathName* pathName = Allocate ( * ( RStringPtr * *) &stringArray, entries );
- ::DeallocatePtr( (Ptr) stringArray );
- return pathName;
- }
-
- /***********************************|****************************************/
-
- void
- TPathName::Deallocate ( PackedPathName*& p )
- {
- if ( p )
- {
- ::DeallocatePtr( (Ptr) p );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( MemError () == noErr );
- #endif
- p = nil;
- }
- }
-
- /***********************************|****************************************/
-
- void
- TPathName::Deallocate ()
- {
- InvalidateCache ();
- Deallocate ( fPacked );
- }
-
- /***********************************|****************************************/
-
- void
- TPathName::ValidateCache () const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN ( IsValidInternal () );
- #endif
-
- if ( !fCache )
- {
- ( (TPathName*) this )->fCache = AllocateArray ( fPacked );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( fCache != nil );
- #endif
- }
- }
-
- /***********************************|****************************************/
-
- void
- TPathName::InvalidateCache () const
- {
- if ( fCache )
- {
- ::DeallocatePtr( (Ptr) ( (TPathName*) this )->fCache );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( MemError () == noErr );
- #endif
- ( (TPathName*) this )->fCache = nil;
- }
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::IsValidIndex ( unsigned short i ) const
- {
- return i >= 1 && i <= GetNodeCount ();
- }
-
- /***********************************|****************************************/
-
- unsigned short TPathName::GetNodeCount () const
- {
- if (fPacked && (fPacked->dataLength > 0))
- return ::OCEDNodeNameCount ( fPacked );
- return 0;
- }
-
- /***********************************|****************************************/
-
- unsigned short
- TPathName::GetPhysicalSize ( const PackedPathName* p )
- {
- return ( p ? p->dataLength : 0 ) + sizeof ( ProtoPackedPathName );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::IsValid ( const PackedPathName* p )
- {
- return ( p != nil ) && ::OCEValidPackedPathName ( p );
- }
-
- /***********************************|****************************************/
-
- #if defined ( INTERNAL_CHECKS )
-
- Boolean
- TPathName::IsValidInternal ( const PackedPathName* p )
- {
- return !gInternalValidityChecking || IsValid ( p );
- }
-
- #endif
-
- /***********************************|****************************************/
-
- TPathName::TPathName ():
- fPacked ( Allocate ( sizeof ( ProtoPackedPathName ) ) ),
- fCache ( nil )
- {
- // ASSERT ( IsValidInternal () ); // we are not valid at this time
- }
-
- pascal char* GetA6 () = 0x2E8E;
-
- #define MAKE_FIRST_PARAM_INTO_TRSTRING_ARRAY() (const TRString**) ( GetA6 () + 0xC )
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const TRString& /* r1 */ ):
- fPacked ( Allocate ( MAKE_FIRST_PARAM_INTO_TRSTRING_ARRAY (), 1 ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const TRString& /* r1 */, const TRString& ):
- fPacked ( Allocate ( MAKE_FIRST_PARAM_INTO_TRSTRING_ARRAY (), 2 ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const TRString& /* r1 */, const TRString&, const TRString& ):
- fPacked ( Allocate ( MAKE_FIRST_PARAM_INTO_TRSTRING_ARRAY (), 3 ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const TPathName& that ):
- fPacked ( Allocate ( that.fPacked ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const PackedPathName& p ):
- fPacked ( Allocate ( &p ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const PackedPathName* p ):
- fPacked ( Allocate ( p ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::TPathName ( const RStringPtr * array, unsigned short count ):
- fPacked ( Allocate ( (RStringPtr *) array, count ) ),
- fCache ( nil )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- /***********************************|****************************************/
-
- TPathName::~TPathName ()
- {
- InvalidateCache ();
- Deallocate ( fPacked );
- }
-
- /***********************************|****************************************/
-
- TPathName&
- TPathName::operator = ( const TPathName& that )
- {
- if ( ASSERT ( this != &that ) )
- {
- Deallocate ();
- fPacked = Allocate ( that.fPacked );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- TPathName&
- TPathName::operator = ( const PackedPathName* p )
- {
- if ( ASSERT ( IsValid ( p ) ) )
- {
- if ( ASSERT ( fPacked != p ) )
- {
- Deallocate ();
- fPacked = Allocate ( p );
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- }
- }
-
- return *this;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::SetNode ( unsigned short oneBasedIndex, const TRString& r )
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
-
- // Check whether this index could be valid.
- ASSERT_RETURN_ZERO (oneBasedIndex >= 1);
-
- // You can't add a pathname item index if there isn't an index-1 item already
- ASSERT_RETURN_ZERO ( oneBasedIndex <= GetNodeCount() + 1);
-
- // Unpack the existing path name
- unsigned short newNodeCount = (GetNodeCount() > oneBasedIndex) ? GetNodeCount() : oneBasedIndex;
- RStringPtr* array = (RStringPtr *) AllocateArray ( fPacked );
- RStringPtr * newArray = (RStringPtr *) FAILNewPtrClear( sizeof(ConstRStringPtr) * newNodeCount);
-
- // Copy the old pathname items over into this new items
- for (unsigned short i = 0; i < GetNodeCount(); ++i)
- newArray[i] = array[i];
- newArray[oneBasedIndex-1] = (RStringPtr) (const RString *) r; // and replace the index-th one.
-
- // Create a new packed path based on these new path items
- PackedPathName* newPackedPath = Allocate ( newArray, newNodeCount );
- ::DeallocatePtr( (Ptr) array );
-
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( MemError () == noErr );
- #endif
-
- // Dispose of the current memory used by this object
- Deallocate ();
- fPacked = newPackedPath;
-
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::GetNode ( unsigned short oneBasedIndex, TRString& r ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT_RETURN_ZERO ( IsValidInternal () );
- #endif
- ASSERT_RETURN_ZERO ( IsValidIndex ( oneBasedIndex ) );
- ValidateCache ();
- ASSERT_RETURN_ZERO ( fCache [ oneBasedIndex ] != nil );
- r = *fCache [ oneBasedIndex - 1 ];
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::operator == ( const TPathName& that ) const
- {
- return ::OCEEqualPackedPathName ( fPacked, that.fPacked );
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::operator == ( const PackedPathName& p ) const
- {
- return ::OCEEqualPackedPathName ( fPacked, &p );
- }
-
- /***********************************|****************************************/
-
- const RString&
- TPathName::operator [] ( unsigned short zeroBasedIndex ) const
- {
- if ( IsValidIndex ( zeroBasedIndex + 1 ) )
- {
- ValidateCache ();
-
- #if defined ( INTERNAL_CHECKS )
- if ( ASSERT ( TRString::IsValid ( fCache [ zeroBasedIndex ], kOCEGenericSensitive ) ) )
- #endif
- return *fCache [ zeroBasedIndex ];
- }
-
- // if we get to here, we are obligated to return something,
- // so let’s return a bogus object (we should probably throw an exception, though…)
-
- static const TRString* gBozoString = nil;
-
- if ( !gBozoString )
- gBozoString = new TRString ( "TPathName::operator [] ( bad index )" );
-
- return *gBozoString;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::Write ( TAbstractFile& f ) const
- {
- unsigned short physicalSize = GetPhysicalSize ( fPacked );
- ASSERT_RETURN_ZERO ( f.Write ( physicalSize ) == noErr );
- ASSERT_RETURN_ZERO ( f.WriteDataIgnore ( fPacked, physicalSize ) == noErr );
- return true;
- }
-
- /***********************************|****************************************/
-
- Boolean
- TPathName::Read ( TAbstractFile& f )
- {
- unsigned short physicalSize = 0;
- ASSERT_RETURN_ZERO ( f.Read ( physicalSize ) == noErr );
- ASSERT_RETURN_ZERO ( physicalSize >= sizeof ( ProtoPackedPathName ) );
- PackedPathName* newPaths = Allocate ( physicalSize );
- ASSERT_RETURN_ZERO ( newPaths != nil );
- ASSERT_RETURN_ZERO ( f.ReadDataIgnore ( newPaths, physicalSize ) == noErr );
- Deallocate ();
- fPacked = newPaths;
- return true;
- }
-
- /***********************************|****************************************/
-
- ostream&
- TPathName::operator >> ( ostream& s ) const
- {
- #if defined ( INTERNAL_CHECKS )
- ASSERT ( IsValidInternal () );
- #endif
- ValidateCache ();
- unsigned short nodes = GetNodeCount ();
-
- for ( unsigned short index = 0; index < nodes; index++ )
- {
- if ( index > 0 )
- s << ":";
-
- s << operator [] ( index );
- }
-
- return s;
- }
-
-